package com.gitee.huanminabc.db_freedom.interceptor;

import com.gitee.huanminabc.db_freedom.wrapper.AbstractBaseWrapper;
import com.gitee.huanminabc.db_freedom.wrapper.LambdaSQL;
import com.gitee.huanminabc.db_freedom.wrapper.SqlWrapperHelper;
import lombok.Data;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;
import java.sql.Connection;
import java.util.Collections;
import java.util.Properties;


//这种方式是不兼容mybatis-plus ,直接对接mybatis , 这样就导致了mybatis-plus的类的注解啥的都不能用了
@Intercepts({@Signature(
        type = StatementHandler.class,
        method = "prepare",
        args = {Connection.class, Integer.class}
), @Signature(
        type = StatementHandler.class,
        method = "getBoundSql",
        args = {}
), @Signature(
        type = Executor.class,
        method = "update",
        args = {MappedStatement.class, Object.class}
), @Signature(
        type = Executor.class,
        method = "query",
        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
), @Signature(
        type = Executor.class,
        method = "query",
        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
)})
@Data
public class FreedomInterceptor_back implements Interceptor {
    @Resource
    @Lazy
    private SqlWrapperHelper sqlWrapperHelper;


    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        Object target = invocation.getTarget();
        if (target instanceof Executor) {
            if (args[0] instanceof MappedStatement) {
                if (args[1] instanceof AbstractBaseWrapper) {
                    MappedStatement ms = (MappedStatement) args[0];
                    Object parameter = args[1];
                    LambdaSQL lambdaSQL = (LambdaSQL) parameter;
                    if (ms.getId().endsWith("selectFreedomList")) {
                       return sqlWrapperHelper.queryList(lambdaSQL,lambdaSQL.getTableInfo().getEntityClass());
                    }else if (ms.getId().endsWith("selectFreedomOne")) {
                        return sqlWrapperHelper.queryOne(lambdaSQL);
                    }else if (ms.getId().endsWith("selectFreedomMaps")) {
                        return sqlWrapperHelper.queryMaps(lambdaSQL);
                    } else if (ms.getId().endsWith("selectFreedomMap")) {
                        return Collections.singletonList(sqlWrapperHelper.queryMap(lambdaSQL));
                    }  else if (ms.getId().endsWith("executeFreedom")) {
                        return sqlWrapperHelper.execute(lambdaSQL);
                    }
                }
            }
        }
        return invocation.proceed();
    }
    @Override
    public Object plugin(Object target) {
        return Interceptor.super.plugin(target);
    }

    @Override
    public void setProperties(Properties properties) {
        Interceptor.super.setProperties(properties);
    }
}